package com.he.hsdyc.controller;

import com.he.hsdyc.common.utils.ValidateUtils;
import com.he.hsdyc.model.msg.ErrorCode;
import com.he.hsdyc.model.msg.Message;
import com.he.hsdyc.model.vo.CourseOrderVo;
import com.he.hsdyc.service.OrderAsyncService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

@RestController
@RequestMapping(value = "async")
public class AsyncController {

    @Autowired
    private OrderAsyncService orderAsyncService;

    private final int threadCount = 4;

    /**
     * 功能描述: 订单统计
     * @param:
     */
    @ApiOperation("订单统计")
    @RequestMapping(value = "countOrder",method = RequestMethod.POST)
    @ApiImplicitParam(name = "schoolId", value = "学校id", dataType = "long", required = true, paramType = "query")
    public Message<Map<String,Object>> countOrder(@RequestBody CourseOrderVo vo) throws ExecutionException, InterruptedException{
        if(ValidateUtils.isIllegalLong(vo.getSchoolId()))
            return new Message<>(ErrorCode.ERROR_ARG);

        long startTime = System.currentTimeMillis();
        //多线程之Future模式，经测试本接口执行时间由78ms优化到4ms;
        Future<Map<String, Object>> mapFutureOrder = orderAsyncService.countOrder(vo);
        Future<Map<String, Object>> mapFutureClassPackage = orderAsyncService.countOrderClassPackage(vo);
        Future<Map<String, Object>> mapFutureOrderGood = orderAsyncService.countOrderGood(vo);
        Future<Map<String, Object>> mapFutureOrderAccount = orderAsyncService.countOrderAccount(vo);
        long endTime = System.currentTimeMillis();

        mapFutureOrder.get().putAll(mapFutureClassPackage.get());
        mapFutureOrder.get().putAll(mapFutureOrderGood.get());
        mapFutureOrder.get().putAll(mapFutureOrderAccount.get());

        System.out.println("多线程任务执行时间:======================="+(endTime-startTime));
        return new Message<>(ErrorCode.SUCCESS,mapFutureOrder.get());

//
//        long startTime = System.currentTimeMillis();
//
//        ExecutorService executorService = Executors.newCachedThreadPool();
//        CountDownLatch countDownLatch = new CountDownLatch(threadCount);
//        for(int i = 0;i<threadCount;i++) {
//            final int threadNum = i;
//            executorService.execute( () -> {
//                if(threadNum == 0) {
//                    Map<String, Object> map1 = orderAsyncService.countOrder2(vo);
//                }
//                if(threadNum == 1) {
//                    Map<String, Object> map2 = orderAsyncService.countOrderClassPackage2(vo);
//                }
//                if(threadNum == 2) {
//                    Map<String, Object> map3 = orderAsyncService.countOrderGood2(vo);
//                }
//                if(threadNum == 3) {
//                    Map<String, Object> map4 = orderAsyncService.countOrderAccount2(vo);
//                }
//            });
//        }
//
//        long endTime = System.currentTimeMillis();
//        System.out.println("多线程任务执行时间:======================="+(endTime-startTime));
//        return new Message<>(ErrorCode.SUCCESS);
    }

}
